/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelCadastroDisciplina.java
 *
 * Created on 23/06/2011, 13:42:47
 */
package telas;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import pacoteDao.*;
import projetofinal.*;

/**
 *
 * @author Iana
 */
public class PainelCadastroDisciplina extends javax.swing.JPanel {

    /** Creates new form PainelCadastroDisciplina */
    public PainelCadastroDisciplina() {
        initComponents();
        carregaProfessor();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonCadastrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jTextFieldNome = new javax.swing.JTextField();
        jComboBoxProfessor = new javax.swing.JComboBox();
        jLabelProfessor = new javax.swing.JLabel();

        jButtonCadastrar.setText("Cadastrar");
        jButtonCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCadastrarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("CADASTRO DE DISCIPLINA");

        jLabelNome.setText("Nome:");

        jComboBoxProfessor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção:" }));
        jComboBoxProfessor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBoxProfessorMouseClicked(evt);
            }
        });
        jComboBoxProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxProfessorActionPerformed(evt);
            }
        });

        jLabelProfessor.setText("Professor:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(jLabel1)
                .addContainerGap(98, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextFieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                .addGap(29, 29, 29))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelProfessor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxProfessor, 0, 171, Short.MAX_VALUE)
                .addGap(187, 187, 187))
            .addGroup(layout.createSequentialGroup()
                .addGap(145, 145, 145)
                .addComponent(jButtonCadastrar)
                .addContainerGap(174, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelProfessor)
                    .addComponent(jComboBoxProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addComponent(jButtonCadastrar)
                .addContainerGap(133, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCadastrarActionPerformed
        // TODO add your handling code here:

        String nome = jTextFieldNome.getText();
        String professor = (String) jComboBoxProfessor.getSelectedItem();

        //ProjDisciplina disciplina = new ProjDisciplina();
        Disciplina disciplina = new Disciplina();

        try {
            DisciplinaDAO disciplinaDao = new DisciplinaDAO();
            disciplinaDao.insert(disciplina);
        } catch (SQLException ex) {
            Logger.getLogger(PainelCadastroDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);
}//GEN-LAST:event_jButtonCadastrarActionPerformed

    private void carregaProfessor() {

        try {
            ProfessorDAO professorDao = new ProfessorDAO();
            List<Professor> professores = professorDao.consultar();
            for (Professor professor : professores) {
                jComboBoxProfessor.addItem(professor.getNome());
            }
        } catch (SQLException ex) {
            Logger.getLogger(PainelCadastroDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jComboBoxProfessorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBoxProfessorMouseClicked
        // TODO add your handling code here:
}//GEN-LAST:event_jComboBoxProfessorMouseClicked

    private void jComboBoxProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxProfessorActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jComboBoxProfessorActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCadastrar;
    private javax.swing.JComboBox jComboBoxProfessor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelProfessor;
    private javax.swing.JTextField jTextFieldNome;
    // End of variables declaration//GEN-END:variables
}
